

using System.Numerics;

namespace NodeBasedSkillEditor.Model.Node.RT
{
    public class Node_Shortest_RT : RT_Node
    {
        public RT_SingleValuePort[] InputVectors_RT;

        public RT_SingleValuePort Result_RT;

        public Node_Shortest_RT(Node_Shortest sourceData, RT_DAG parentDag) : base(sourceData, parentDag)
        {
            InputVectors_RT = BuildRTPortArrayFromGroup<RT_SingleValuePort>(EPortIOType.Input, sourceData.InputVectors);

            Result_RT = new RT_SingleValuePort(EPortIOType.Output, sourceData.Result, this);
            Result_RT.OnGetValue = GetResult;

        }

        public AValue GetResult(DAG_Executor executor)
        {
            if (InputVectors_RT.Length > 0)
            {
                var vecType = Result_RT.SourceValue.ValueType;
                if (vecType == AValue.EValueType.Vector2)
                {
                    Vector2 result = InputVectors_RT[0].GetValue(executor).ToVector2().Item2;
                    float shortestLenSq = result.LengthSquared();
                    for (int i = 1; i < InputVectors_RT.Length; i++)
                    {
                        Vector2 currVec2 = InputVectors_RT[i].GetValue(executor).ToVector2().Item2;
                        float currLenSq = currVec2.LengthSquared();
                        if (shortestLenSq > currLenSq)
                        {
                            result = currVec2;
                            shortestLenSq = currLenSq;
                        }
                    }
                    Result_RT.SourceValue.SetVector2(result);
                }
                else if (vecType == AValue.EValueType.Vector3)
                {
                    Vector3 result = InputVectors_RT[0].GetValue(executor).ToVector3().Item2;
                    float shortestLenSq = result.LengthSquared();
                    for (int i = 1; i < InputVectors_RT.Length; i++)
                    {
                        Vector3 currVec3 = InputVectors_RT[i].GetValue(executor).ToVector3().Item2;
                        float currLenSq = currVec3.LengthSquared();
                        if (shortestLenSq > currLenSq)
                        {
                            result = currVec3;
                            shortestLenSq = currLenSq;
                        }
                    }
                    Result_RT.SourceValue.SetVector3(result);
                }
            }
            return Result_RT.SourceValue;

        }


    }
}